﻿#include "EditorAbilityGroupModel.h"
#include "Object/NewObject.h"
#include "Widgets/Splitable/EditorSplitableLayoutModel.h"

void REditorAbilityGroupModel::FirstTimeInitializeObject()
{
    Super::FirstTimeInitializeObject();

    LayoutModel=NewObject<REditorSplitableLayoutModel>(this);
  
}

// int64_t REditorAbilityGroupModel::GetAbilitiesNum() const
// {
//     return Abilities.Num();
// }

// TSharedObjectPtr<REditorAbilityModel> REditorAbilityGroupModel::GetAbility(int64_t Index) const
// {
//     return Abilities[Index];
// }

void REditorAbilityGroupModel::UpdateLayout()
{
    if(Abilities.Num()==0)
    {
        LayoutModel->SetLayoutTree(nullptr);
        return;
    }

    if(Abilities.Num()==1)
    {
        auto AbilityNode=NewObject<REditorSplitableLayoutLeafNode>(LayoutModel);
        AbilityNode->ItemWidgetModel=Abilities[0];
        LayoutModel->SetLayoutTree(AbilityNode);
        return;
    }

    auto LayoutRootNode=NewObject<REditorContainerSplitableLayoutNode>(LayoutModel);
    LayoutRootNode->SetVertical();
    for(int64_t i=0;i<Abilities.Num();i++)
    {
        auto LayoutSubnode=NewObject<REditorSplitableLayoutLeafNode>(LayoutRootNode);
        LayoutSubnode->ItemWidgetModel=Abilities[i];

        LayoutRootNode->AddChild(
            LayoutSubnode
            ,1
            ,GetAbilityTitleHeight()
            ,GetAbilityTitleHeight());
    }
    LayoutModel->SetLayoutTree(LayoutRootNode);
}

float REditorAbilityGroupModel::GetAbilityTitleHeight() const
{
    return 20.0f;
}

TSharedObjectPtr<REditorSplitableLayoutModel> REditorAbilityGroupModel::GetLayout()
{
    return LayoutModel;
}
